package com.udp;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


public class UdpSend {

    public static void main(String[] args) throws Exception {
        //创建一个socket数据报文对象
        DatagramSocket socket = new DatagramSocket(6060);

        //定时发送
        while (true) {
            /*
             * 建数据包，数据+ip地址+端口号
             * */
            System.out.print("当前用户：");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String msg = reader.readLine();
            InetAddress localhost = InetAddress.getByName("localhost");
            int port = 9090;
            //打包
            DatagramPacket packet = new DatagramPacket
                    (msg.getBytes(),0, msg.getBytes().length,localhost,port);
            socket.send(packet);
            if ("bye".equals(msg)){
                socket.close();
                break;
            }
        }

    }

}
